package com.hnkywl.jip.action;

import com.google.gson.Gson;
import com.hnkywl.jip.service.*;
import com.hnkywl.jip.utils.BankData;
import com.hnkywl.jip.utils.CompanyData;
import com.hnkywl.jip.utils.UserData;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 说明：
 * 包:        com.hnkywl.jip.action
 * 类描述:    [一句话描述该类的功能]
 * 创建人:    ZHUCHG
 * 创建时间: 13-2-28 下午9:02
 * 修改人:    ZHUCHG
 * 修改时间: 13-2-28 下午9:02
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Namespace("/cfg")
public class SupplierAction extends SimpleActionSupport {
    private Integer companyId;
    private Integer supplierId;
    private String code;

    private String key;

    private String data;

    private String userName;

    private Integer limit;
    private Integer start;

    private List<Object[]> companyList;

    private Page<Object[]> gridResult;

    private SupplierService supplierService;

    private CompanyService companyService;

    private UserService userService;

    @Action("supplier_mapping")
    public String supplierMapping() {
        companyList = supplierService.findCompany();
        return SUCCESS;
    }

    @Action("supplier_grid_json")
    public String supplierGridJson() {
        gridResult = supplierService.findSupplier(companyId, supplierId, code, limit, start);
        return SUCCESS;
    }

    @Action("save_supplier_mapping")
    public void saveSupplierMapping() {
        try {
            supplierService.saveSupplierMapping(companyId, supplierId, code);
            renderJson("{\"success\":true,\"message\":\"保存成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"success\":true,\"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("other_mapping")
    public String otherMapping() {
        this.companyList = this.companyService.searchAllCompany();
        return SUCCESS;
    }

    @Action("other_grid_json")
    public String otherGridJson() {
        if(companyId!=null) {
            gridResult = this.companyService.searchCompanyMapping(companyId, start, limit);
        }
        return SUCCESS;
    }

    @Action("save_other_grid")
    public void saveOtherGrid() {
        Gson gson = new Gson();
        java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<CompanyData>>() {
        }.getType();
        List<CompanyData> otherDataList = gson.fromJson(this.data, type);
        this.companyService.saveCompanyMapping(otherDataList);
    }

    @Action("user_mapping")
    public String userMapping() {
        return SUCCESS;
    }

    @Action("user_grid_json")
    public String userGridJson() {
        gridResult = this.userService.searchUserMapping(userName, start, limit);
        return SUCCESS;
    }

    @Action("save_user_grid")
    public void saveUserGrid() {
        Gson gson = new Gson();
        java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<UserData>>() {
        }.getType();
        List<UserData> userDataList = gson.fromJson(this.data, type);
        this.userService.saveUserMapping(userDataList);
    }

    //===========setter getter=============
    public Integer getCompanyId() {
        return companyId;
    }

    public void setCompanyId(Integer companyId) {
        this.companyId = companyId;
    }

    public Integer getSupplierId() {
        return supplierId;
    }

    public void setSupplierId(Integer supplierId) {
        this.supplierId = supplierId;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    //================setter=============
    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public void setStart(Integer start) {
        this.start = start;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    //=============getter================
    public Page<Object[]> getGridResult() {
        return gridResult;
    }

    public List<Object[]> getCompanyList() {
        return companyList;
    }



    @Autowired
    public void setSupplierService(SupplierService supplierService) {
        this.supplierService = supplierService;
    }


    @Autowired
    public void setCompanyService(CompanyService companyService) {
        this.companyService = companyService;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
